#include "StdAfx.h"
#include "ResourceManager.h"
#include "ScalableUnitAssist.h"

extern const int s_SmallNodeHalfLength = 4;

void CScalableUnitAssist::draw( HDC p_dc, const CRect& p_rect )
{
	//TRACE("draw CScalableUnitAssist");
	// 	RECT leftUp, middleUp, rightUp,
	// 		 leftMiddle, rightMiddle,
	// 		 leftBottom, middleBottom, rightBottom;
	HPEN   hOldPen	 = (HPEN)SelectObject( p_dc, CResourceManager::getBlackLinePen() );
	HBRUSH hOldbrush = (HBRUSH)SelectObject( p_dc, CResourceManager::getGreenBrush() );

	int halfWidthPos  = ( p_rect.right  + p_rect.left ) / 2;
	int halfHeightPos = ( p_rect.bottom + p_rect.top )  / 2;
	//draw leftUp smallNode
	::Rectangle( p_dc, 
		p_rect.left - s_SmallNodeHalfLength,
		p_rect.top  - s_SmallNodeHalfLength,
		p_rect.left + s_SmallNodeHalfLength,
		p_rect.top  + s_SmallNodeHalfLength );
	//draw middleUp smallNode
	::Rectangle( p_dc, 
		halfWidthPos - s_SmallNodeHalfLength,
		p_rect.top   - s_SmallNodeHalfLength,
		halfWidthPos + s_SmallNodeHalfLength,
		p_rect.top   + s_SmallNodeHalfLength );
	//draw rightUp smallNode
	::Rectangle( p_dc, 
		p_rect.right - s_SmallNodeHalfLength,
		p_rect.top   - s_SmallNodeHalfLength,
		p_rect.right + s_SmallNodeHalfLength,
		p_rect.top   + s_SmallNodeHalfLength );
	//draw leftMiddle smallNode
	::Rectangle( p_dc, 
		p_rect.left   - s_SmallNodeHalfLength,
		halfHeightPos - s_SmallNodeHalfLength,
		p_rect.left   + s_SmallNodeHalfLength,
		halfHeightPos + s_SmallNodeHalfLength );
	//draw rightMiddle smallNode
	::Rectangle( p_dc, 
		p_rect.right  - s_SmallNodeHalfLength,
		halfHeightPos - s_SmallNodeHalfLength,
		p_rect.right  + s_SmallNodeHalfLength,
		halfHeightPos + s_SmallNodeHalfLength );
	//draw leftBottom smallNode
	::Rectangle( p_dc, 
		p_rect.left   - s_SmallNodeHalfLength,
		p_rect.bottom - s_SmallNodeHalfLength,
		p_rect.left   + s_SmallNodeHalfLength,
		p_rect.bottom + s_SmallNodeHalfLength );
	//draw middleBottom smallNode
	::Rectangle( p_dc, 
		halfWidthPos  - s_SmallNodeHalfLength,
		p_rect.bottom - s_SmallNodeHalfLength,
		halfWidthPos  + s_SmallNodeHalfLength,
		p_rect.bottom + s_SmallNodeHalfLength );
	//draw rightBottom smallNode
	::Rectangle( p_dc, 
		p_rect.right  - s_SmallNodeHalfLength,
		p_rect.bottom - s_SmallNodeHalfLength,
		p_rect.right  + s_SmallNodeHalfLength,
		p_rect.bottom + s_SmallNodeHalfLength );

	SelectObject( p_dc, hOldPen );
	SelectObject( p_dc, hOldbrush );
}
//////////////////////////////////////////////////////////////////////////
void CScalableUnitAssist::drawSelected(HDC p_dc, const CRect& p_rect, Shape_Type shapeType)
{
	HPEN   hOldPen;
	HBRUSH hOldBrush;

	SetBkMode( p_dc, TRANSPARENT );
	hOldPen   = (HPEN)::SelectObject( p_dc, CResourceManager::getGreenLineDotPen() );
	hOldBrush = (HBRUSH)::SelectObject( p_dc, CResourceManager::getBlankBrush() );
	if (shapeType == ST_RECTANGLE)
	{
		::Rectangle( p_dc, p_rect.left, p_rect.top ,p_rect.right, p_rect.bottom );
	}
	if (shapeType == ST_DIAMOND)
	{
		int xMid = (p_rect.top + p_rect.bottom ) / 2;
		int yMid = (p_rect.left + p_rect.right ) / 2;	

		CPoint poly[4] = 
		{ 
			CPoint( p_rect.left, xMid ) , 
			CPoint( yMid,p_rect.top  ), 
			CPoint( p_rect.right, xMid ),
			CPoint( yMid, p_rect.bottom )
		};
		::Polygon( p_dc , poly , 4 );
	}
	
	//draw( p_dc, p_rect );
	::SelectObject( p_dc, hOldBrush );
	::SelectObject( p_dc, hOldPen );
}

bool CScalableUnitAssist::isPointNearby( const CRect& p_rect, const CPoint& p_point )
{
	return false;
}



bool CScalableUnitAssist::isOnLeftSmallNode( const CRect& p_rect, const CPoint& p_point )
{
	int halfWidthPos  = ( p_rect.left  + p_rect.right ) / 2;
	int halfHeightPos = ( p_rect.top + p_rect.bottom )  / 2;
	return (p_point.x >= p_rect.left   - s_SmallNodeHalfLength) &&
		   (p_point.x <= p_rect.left   + s_SmallNodeHalfLength) &&
		   (p_point.y >= halfHeightPos - s_SmallNodeHalfLength) &&
		   (p_point.y <= halfHeightPos + s_SmallNodeHalfLength);
}

bool CScalableUnitAssist::isOnTopSmallNode( const CRect& p_rect, const CPoint& p_point )
{
	int halfWidthPos  = ( p_rect.left  + p_rect.right ) / 2;
	int halfHeightPos = ( p_rect.top + p_rect.bottom )  / 2;

	return (p_point.x >= halfWidthPos - s_SmallNodeHalfLength) &&
		   (p_point.x <= halfWidthPos + s_SmallNodeHalfLength) &&
		   (p_point.y >= p_rect.top	  - s_SmallNodeHalfLength) &&
		   (p_point.y <= p_rect.top	  + s_SmallNodeHalfLength);
}

bool CScalableUnitAssist::isOnRightSmallNode( const CRect& p_rect, const CPoint& p_point )
{
	int halfWidthPos  = ( p_rect.left  + p_rect.right ) / 2;
	int halfHeightPos = ( p_rect.top + p_rect.bottom )  / 2;

	return (p_point.x >= p_rect.right  - s_SmallNodeHalfLength) &&
		   (p_point.x <= p_rect.right  + s_SmallNodeHalfLength) &&
		   (p_point.y >= halfHeightPos - s_SmallNodeHalfLength) &&
		   (p_point.y <= halfHeightPos + s_SmallNodeHalfLength);
}

bool CScalableUnitAssist::isOnBottomSmallNode( const CRect& p_rect, const CPoint& p_point )
{
	int halfWidthPos  = ( p_rect.left + p_rect.right )  / 2;
	int halfHeightPos = ( p_rect.top  + p_rect.bottom ) / 2;

	return (p_point.x >= halfWidthPos  - s_SmallNodeHalfLength) &&
		   (p_point.x <= halfWidthPos  + s_SmallNodeHalfLength) &&
		   (p_point.y >= p_rect.bottom - s_SmallNodeHalfLength) &&
		   (p_point.y <= p_rect.bottom + s_SmallNodeHalfLength);
}

bool CScalableUnitAssist::isOnCenterSmallNode( const CRect& p_rect, const CPoint& p_point )
{
	int halfWidthPos  = ( p_rect.left + p_rect.right )  / 2;
	int halfHeightPos = ( p_rect.top  + p_rect.bottom ) / 2;

	return abs(p_point.x - halfWidthPos  ) <= p_rect.Width()  / 6 &&
		   abs(p_point.y - halfHeightPos ) <= p_rect.Height() / 6;
}
Anchor_Pos CScalableUnitAssist::AnchorSelected(const CRect& rect, const CPoint& point)
{
	int halfWidthPos  = ( rect.left  + rect.right ) / 2;
	int halfHeightPos = ( rect.top + rect.bottom )  / 2;


	//topleft
	if ( (point.x >= rect.left - s_SmallNodeHalfLength) &&
		(point.x <= rect.left + s_SmallNodeHalfLength) &&
		(point.y >= rect.top - s_SmallNodeHalfLength) &&
		(point.y <= rect.top + s_SmallNodeHalfLength) )
	{
		return AP_TOPLEFT;
	}

	//topmid
	if ( (point.x >= halfWidthPos - s_SmallNodeHalfLength) &&
		(point.x <= halfWidthPos + s_SmallNodeHalfLength) &&
		(point.y >= rect.top - s_SmallNodeHalfLength) &&
		(point.y <= rect.top + s_SmallNodeHalfLength) )
	{
		return AP_TOPMID;
	}

	//topright
	if ( (point.x >= rect.right - s_SmallNodeHalfLength) &&
		(point.x <= rect.right + s_SmallNodeHalfLength) &&
		(point.y >= rect.top - s_SmallNodeHalfLength) &&
		(point.y <= rect.top + s_SmallNodeHalfLength) )
	{
		return AP_TOPRIGHT;
	}

	//midleft
	if ( (point.x >= rect.left - s_SmallNodeHalfLength) &&
		(point.x <= rect.left + s_SmallNodeHalfLength) &&
		(point.y >= halfHeightPos - s_SmallNodeHalfLength) &&
		(point.y <= halfHeightPos + s_SmallNodeHalfLength) )
	{
		return AP_MIDLEFT;
	}


	//midright
	if ( (point.x >= rect.right - s_SmallNodeHalfLength) &&
		(point.x <= rect.right + s_SmallNodeHalfLength) &&
		(point.y >= halfHeightPos - s_SmallNodeHalfLength) &&
		(point.y <= halfHeightPos + s_SmallNodeHalfLength) )
	{
		return AP_MIDRIGHT;
	}

	//bottomleft
	if ( (point.x >= rect.left - s_SmallNodeHalfLength) &&
		(point.x <= rect.left + s_SmallNodeHalfLength) &&
		(point.y >= rect.bottom - s_SmallNodeHalfLength) &&
		(point.y <= rect.bottom + s_SmallNodeHalfLength) )
	{
		return AP_BOTTOMLEFT;
	}

	//bottommid
	if ( (point.x >= halfWidthPos - s_SmallNodeHalfLength) &&
		(point.x <= halfWidthPos + s_SmallNodeHalfLength) &&
		(point.y >= rect.bottom - s_SmallNodeHalfLength) &&
		(point.y <= rect.bottom + s_SmallNodeHalfLength) )
	{
		return AP_BOTTOMMID;
	}

	//bottomright
	if ( (point.x >= rect.right - s_SmallNodeHalfLength) &&
		(point.x <= rect.right + s_SmallNodeHalfLength) &&
		(point.y >= rect.bottom - s_SmallNodeHalfLength) &&
		(point.y <= rect.bottom + s_SmallNodeHalfLength) )
	{
		return AP_BOTTOMRIGHT;
	}

	if ((point.x < rect.left)||(point.x > rect.right)||(point.y < rect.top)||(point.y > rect.bottom))
	{
		return AP_NOTIN;
	}
	return AP_COMMON;

}